package org.gp.core.utils;

import okhttp3.*;

import java.io.IOException;

/**
 * Created by luoliang on 2017/1/24.
 * Okhttp工具类，用于向微信服务器发送get，post请求
 */
public class OkHttpUtil {
    private static OkHttpClient okHttpClient = new OkHttpClient();
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static String get(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = okHttpClient.newCall(request).execute();

        return response.body().string();
    }

    public static String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = okHttpClient.newCall(request).execute();

        return response.body().string();
    }
}
